package com.evildog.workspace.life.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.util.WsAssert;
import com.evildog.workspace.dependencies.mybatis.enums.MybatisCustomExceptionDesc;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.WishBO;
import com.evildog.workspace.life.application.bo.WishListBO;
import com.evildog.workspace.life.application.bo.WishPageBO;
import com.evildog.workspace.life.application.convert.WishConvert;
import com.evildog.workspace.life.application.dto.WishDTO;
import com.evildog.workspace.life.application.dto.WishListDTO;
import com.evildog.workspace.life.application.dto.WishPageDTO;
import com.evildog.workspace.life.application.dto.WishStatusDTO;
import com.evildog.workspace.life.application.entity.Wish;
import com.evildog.workspace.life.application.entity.WishListDO;
import com.evildog.workspace.life.application.entity.WishPageDO;
import com.evildog.workspace.life.application.enums.WishStatusEnum;
import com.evildog.workspace.life.application.mapper.WishMapper;
import com.evildog.workspace.life.application.service.WishService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

@Service
@AllArgsConstructor
public class WishServiceImpl extends ServiceImpl<WishMapper, Wish> implements WishService {

    @Override
    public PagedResult<WishPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, WishPageDTO pageDTO) {
        IPage<WishPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                pageDTO);
        return WishConvert.INSTANCE.convert(page);
    }

    @Override
    public List<WishListBO> getList(List<QueryConditionDTO> queryCondition, WishListDTO dto) {
        List<WishListDO> wishList = this.baseMapper.getList(queryCondition, dto);
        return WishConvert.INSTANCE.convert(wishList);
    }

    @Override
    public WishBO getDetailById(Long id) {
        Wish wish = getById(id);
        Assert.notNull(wish, "wish is null!");
        return WishConvert.INSTANCE.convert(wish);
    }

    @Override
    public boolean save(WishDTO dto) {
        Wish newWish = WishConvert.INSTANCE.convert(dto);
        newWish.setStatus(WishStatusEnum.WISHING.getStatus());

        return save(newWish);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, WishStatusDTO convert) {
        Wish oldWish = getById(id);
        Assert.notNull(oldWish, "wish is null!");

        Integer status = convert.getStatus();
        WsAssert.contains(status, MybatisCustomExceptionDesc.INVALID_STATUS,
                WishStatusEnum.CANCEL.getStatus(), WishStatusEnum.COMPLETED.getStatus());

        Wish newWish = new Wish().setStatus(status)
                .setRemoveTime(LocalDateTime.now())
                .setId(id);
        return updateById(newWish);
    }

    @Override
    public boolean updateName(Long id, WishDTO convert) {
        return updateById(id, convert);
    }

    @Override
    public boolean updateCost(Long id, WishDTO convert) {
        return updateById(id, convert);
    }

    @Override
    public boolean updateStar(Long id, WishDTO convert) {
        return updateById(id, convert);
    }

    private boolean updateById(Long id, WishDTO dto) {
        Wish oldWish = getById(id);
        Assert.notNull(oldWish, "wish is null!");
        Wish newWish = WishConvert.INSTANCE.convert(dto);
        newWish.setId(id);
        return updateById(newWish);
    }
}
